//
//  LoginViewController.m
//  Restaurant
//
//  Created by wanghuan on 15/4/20.
//  Copyright (c) 2015年 wanghuan. All rights reserved.
//

#import "LoginViewController.h"

@interface LoginViewController ()

@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    self.view.backgroundColor = AppBackGroundColor;
    
    _manager = [[AFHTTPRequestOperationManager alloc]init];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    
    
}

#pragma mark - MD5加密

- (NSString *)MD5:(NSString *)sourceString {
    
    const char *cStr = [sourceString UTF8String];
    
    unsigned char digest[CC_MD5_DIGEST_LENGTH];
    
    CC_MD5(cStr, (unsigned)strlen(cStr), digest);
    
    NSMutableString *result = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH*2];
    
    for (int i = 0; i < CC_MD5_DIGEST_LENGTH; i++) {
        [result appendFormat:@"%02x",digest[i]];
        
    }
    
    return  result;
}


- (IBAction)LoginBtn:(UIButton *)sender {
    
    [self.view endEditing:YES];
    
    if ([self.passwordText.text length]&&[self.phoneNumText.text length]) {
        
        if ([verifyPassAndPhone checkPassword:self.passwordText.text] && [verifyPassAndPhone checkPhoneNumber:self.phoneNumText.text]) {
            
            _manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/json",@"text/html",@"application/json", nil];
            
            NSString *password = [self MD5:self.passwordText.text];
            
            NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:self.phoneNumText.text,@"telephone",password,@"password", nil];
            
            [self LoginClick:dict];
            
        } else {
        
            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"手机或密码格式不正确" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            
            [alert show];
        
        }
    } else {
    
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"手机号或密码不能为空" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        
        [alert show];
    }
    
}

- (void)LoginClick:(NSDictionary *)dict {

    [_manager POST:LoginUrl parameters:dict success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        //登陆成功
        if ([[responseObject objectForKey:@"success"]boolValue]) {
            
            NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
            
            [defaults setObject:[[responseObject objectForKey:@"obj"] objectForKey:usersId] forKey:usersId];
            
            [defaults setObject:[[responseObject objectForKey:@"obj"] objectForKey:Usertelephone] forKey:Usertelephone];
            
            [defaults setObject:[[responseObject objectForKey:@"obj"] objectForKey:Userpassword] forKey:Userpassword];
            
            [defaults synchronize];
            
            
            [self.navigationController popViewControllerAnimated:YES];
            
        } else {
            
            //登录失败
            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:[responseObject objectForKey:@"code"] delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            
            [alert show];
        }
        
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"连接超时" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        
        [alert show];
        
    }];


}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {

    self.passwordText.text = @"";

}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

    [self.view endEditing:YES];

}

- (IBAction)LookForPSWBtn:(UIButton *)sender {
    
    
}

- (IBAction)RegisterBtn:(UIButton *)sender {
    
    RegisterViewController *rvc = [[RegisterViewController alloc]init];
    
    rvc.viewController = self.viewController;
    
    [self.navigationController pushViewController:rvc animated:YES];
    
}

- (IBAction)backClick:(UIButton *)sender {
    
    [self.navigationController popViewControllerAnimated:YES];
}
@end
